package com.carbohydrate42.kotlin_compose_demo.viewModel

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.carbohydrate42.kotlin_compose_demo.api.TestApi
import kotlinx.coroutines.launch

class TryCoroutineViewModel() : ViewModel() {

    var jsonString: String by mutableStateOf("")
        private set

    var count: Int by mutableStateOf(0)
        private set

    var count2: Int by mutableStateOf(0)
        private set

    fun runTestApi() {
        viewModelScope.launch {
            val result = TestApi.instance.test1(count.toString()).body()
            jsonString = result?.data ?: "没有"
        }
    }

    fun changeJsonStr() {
        jsonString = if (jsonString.isEmpty()) {
            "123123123"
        } else {
            ""
        }
    }

    fun countInc() {
        count++
    }

    fun count2Inc() {
        count2++
    }

}
